<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/icon.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/yzr_style.css"/>">

<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<title>药库盘点</title>
</head>
<body>
<script type="text/javascript">
	$(function(){
		//获取当前登录用户的id
		var  pandianCky_staffId = $("#tontyiStaffId").html();
		
		//打开选择药品窗口
		$("#yzr-pandian-btn").linkbutton({onClick: function(){
			
			var yzr_cangku = $("#yzr-pandian-store").combogrid("getValue");
			if(yzr_cangku == ''){
				$.messager.alert("提示", "请选择盘点仓库！");
			}else{
				$("#yzr-selectMed-window").window("open");
				$("#tb-medStore-selectMed").datagrid({
		    		url: 'queryMedByStore_yzr', 
		    		queryParams: {
		    			cangku: yzr_cangku
		    		},
		    		method: 'get'
		    	});
				$("#yzr-pandian-staff").combogrid("setValue",pandianCky_staffId);
			}
			
			
		}});
		
		//显示全部
		$("#yzr-selectMed-reload").linkbutton({onClick:function(){
			var yzr_cangku = $("#yzr-pandian-store").combogrid("getValue");
			$("#tb-medStore-selectMed").datagrid({
	    		url: 'queryMedByStore_yzr', 
	    		queryParams: {
	    			cangku: yzr_cangku
	    		},
	    		method: 'get'
	    	});			
		}});
			
			
		//搜索
		$('#yzr-selectMed-search').searchbox({			
		    searcher:function(value,name){
		    	var yzr_cangku = $("#yzr-pandian-store").combogrid("getValue");
		    	if(value == ''){
		    		$.messager.alert("提示", "请输入查询条件！");
		    	}else{
		    		$("#tb-medStore-selectMed").datagrid({	    		
			    		url: 'queryMedByName_yzr',
			    		method: 'post', 
			    		queryParams: {
			    			page:1,
			    			rows:13,
			    			medName: value,
			    			cangku: yzr_cangku
			    	}});
		    		$('#yzr-selectMed-search').searchbox('setValue', '');
		    	}
		    	
		    }
		}); 
		
		
		
		
		
		//药品编号，药品名，批号，单位，库存数量，结存数量，盈亏数量 
		var yzr_pandianMed = undefined;//药品名
		var yzr_pihao = undefined;//批号
		var yzr_storeNum = undefined;//库存数量
		
		var yzr_medId = undefined;//药品id
		var yzr_medUnit = undefined;//单位
		
		//确定药品
		$("#yzr-ok-btn-selectMed").linkbutton({onClick: function(){
			var rows = $("#tb-medStore-selectMed").datagrid("getSelections");
			$("#yzr_pandian_form").form("validate");
			if(rows.length == 1){
				 yzr_pandianMed = rows[0].medStockMedName;
				 yzr_pihao = rows[0].medStockBatchnumber;
				 yzr_storeNum = rows[0].medStockNumber;
				 yzr_medId = rows[0].medStockMedId;
				 yzr_medUnit = rows[0].medStockUnit;
				
				$("#yzr-pandian-med").textbox("setValue",yzr_pandianMed);
				$("#yzr-pandian-pihao").textbox("setValue",yzr_pihao);
				$("#yzr-pandian-kcnumber").textbox("setValue",yzr_storeNum);
				$("#yzr-pandian-yaopinId").textbox("setValue",yzr_medId);
				$("#yzr-pandian-yaopinUnit").textbox("setValue",yzr_medUnit);
				$("#yzr-selectMed-window").window("close");
				
				
				 yzr_pandianMed = undefined;
				 yzr_pihao = undefined;
				 yzr_storeNum = undefined;
				 yzr_medId = undefined;
				 yzr_medUnit = undefined;	 
			
			}else{
				$.messager.alert("提示", "您还没有选择记录！");
			}
		}});
		
		var yzr_yinkui = undefined;	 
		//算出盈亏金额
		$("#yzr-pandian-jiecunnumber").numberbox({onChange: function(){			
			var yzr_a = $("#yzr-pandian-kcnumber").textbox("getValue");
			var yzr_b = $("#yzr-pandian-jiecunnumber").numberbox("getValue");

			if(eval(yzr_a) > eval(yzr_b)){
				yzr_yinkui = yzr_a - yzr_b;
			}else{
				yzr_yinkui = yzr_b - yzr_a;
			}
		}});
		
		//添加数据到datagrid
		$("#yzr-pandian-submit").linkbutton({onClick: function(){
			
			//验证表单是否完善
			if($('#yzr_pandian_form').form("validate")){
				//添加行
				$('#yzr-pandianDet-dg').datagrid('appendRow',{
					storeCountDetMedId: $("#yzr-pandian-yaopinId").textbox("getValue"),
					storeCountDetMedName: $("#yzr-pandian-med").textbox("getValue"),
					storeCountDetBatchnumber: $("#yzr-pandian-pihao").textbox("getValue"),
					storeCountDetMedUnit: $("#yzr-pandian-yaopinUnit").textbox("getValue"),
					storeCountDetStockNumber:$("#yzr-pandian-kcnumber").textbox("getValue"),
					storeCountDetInputNum: $("#yzr-pandian-jiecunnumber").numberbox("getValue"),
					storeCountDetYkNum: yzr_yinkui
				});
				//清除部分数据
				$("#yzr-pandian-med").textbox("clear");
				$("#yzr-pandian-pihao").textbox("clear");
				$("#yzr-pandian-kcnumber").textbox("clear");
				$("#yzr-pandian-yaopinId").textbox("clear");
				$("#yzr-pandian-yaopinUnit").textbox("clear");			
				$("#yzr-pandian-jiecunnumber").numberbox("clear");
				yzr_yinkui = undefined;
			}else{
				$.messager.alert("提示", "请填写所有信息！");
			}		
		}});
		
		//删除行
		$("#yzr-pandian-cancel").linkbutton({onClick: function(){
			var rows = $("#yzr-pandianDet-dg").datagrid("getSelections");			
			var yzr_index =$('#yzr-pandianDet-dg').datagrid('getRowIndex', rows[0]);
			$('#yzr-pandianDet-dg').datagrid('deleteRow',yzr_index);
		}});
		
		
		//提交数据到数据库
		$("#yzr-pandianDet-tijiao").linkbutton({onClick: function(){
				
			var rows = $("#yzr-pandianDet-dg").datagrid("getRows");
			console.log(JSON.stringify(rows));
			
			//提交的数据：盘点人，仓库
			var yzr_pdrck = $("#yzr-pandian-store").combogrid("getValue");
			var yzr_pdr = $("#yzr-pandian-staff").combogrid("getValue");			
			
			$("<form id='cc_add_form_1' method='post'>").appendTo($("body"));
			
			
			//var ff_plan = $("#cc_add_form_1");
			//药品编号，药品名称，批号，单位，库存数量，结存数量，盈亏数量
			for(var i = 0; i < rows.length; i ++){
				generateHiddenYzr_pandian(rows[i], "storeCountDetMedId", i);
				generateHiddenYzr_pandian(rows[i], "storeCountDetMedName", i);
				generateHiddenYzr_pandian(rows[i], "storeCountDetBatchnumber", i);
				generateHiddenYzr_pandian(rows[i], "storeCountDetMedUnit", i);
				generateHiddenYzr_pandian(rows[i], "storeCountDetStockNumber", i);
				generateHiddenYzr_pandian(rows[i], "storeCountDetInputNum", i);
				generateHiddenYzr_pandian(rows[i], "storeCountDetYkNum", i);
			}
		
			
			$('#cc_add_form_1').form('submit', {
				
				url: 'addPandianYzr',
				queryParams: {
					pdrck: yzr_pdrck,
					pdr: yzr_pdr
	    		}, 
				/* onSubmit: function(){
			    	return $('#yzr_pandian_form').form("validate");
			    },  */
				success: function(v){
					$.messager.alert('提示', "提交成功");
					$(":hidden[x='y']").remove();			
					$('#yzr_pandian_form').form("clear");
					$('#yzr-pandianDet-dg').datagrid('loadData', { total: 0, rows: [] }); 
				}
			});
			
		}});

		//name=medPurchDets[0] .属性名称
		//value = row[属性名称]
		//创建隐式表单域来保存明细
		function generateHiddenYzr_pandian(row, name, i){
			var ff_plan = $('#cc_add_form_1');
			//type='hidden'
			$("<input type='hidden' x='y'>")
				.attr("name", "storeList["+i+"]."+name)
				.val(row[name])
				.appendTo(ff_plan);
		}
		
	});
</script>


<script type="text/javascript">
	$(function(){
		//打开窗口
		$("#yzr-count-detail-more").linkbutton({onClick: function(){
			
			var rows = $("#yzr-count-detail-dg").datagrid("getSelections");
			if(rows.length == 1){
				var countId = rows[0].storeCountId;
				$("#yzr-countMed-detail-window").window("open");
				
				$.getJSON("queryCountByIdYzr", {"countId": countId}, function(data){
					//storeCountId,storeCountDate,storeCountPerson,storeCountStoreId
					//yzr-countMed-detail-pdbh，yzr-countMed-detail-pdck，yzr-intock-detail-pdr，yzr-intock-detail-pdrq
					$("#yzr-countMed-detail-pdbh").linkbutton({text: data.storeCountId});
					$("#yzr-countMed-detail-pdck").linkbutton({text: data.storeName});
					$("#yzr-intock-detail-pdr").linkbutton({text: data.staffName});
					$("#yzr-intock-detail-pdrq").linkbutton({text: data.storeCountDate});
					
				});	
				
				 //根据id查看详情	
				 $("#yzr-countMed-detail-dg").datagrid({
					url:'queryCountDetByCountIdYzr',
					width: '100%',
					queryParams:{
						countId: countId
					}
				});
			}else{
				$.messager.alert("提示", "请选择一条记录!");
			}
		}});
		
		$('#yzr-pandianDet-tabs').tabs({
		    onSelect:function(title,index){
		        
		        if(title == '盘点药品'){
		        	$("#yzr-display-count-none").show();
		        }else if(title == '查看盘点详情'){
		        	$("#yzr-display-count-none").css("display","none");
		        }
		    }
		});
	});
</script>

	



		<div style="display:none;">
			<input id="yzr-pandian-yaopinId" class="easyui-textbox" 
			data-options="labelAlign:'right',label:'药品编号'"/>
			
			<input id="yzr-pandian-yaopinUnit" class="easyui-textbox" 
			data-options="labelAlign:'right',label:'单位'"/>
		</div>


	<!-- 盘点药库、盘点人、盘点日期
	药品、批号、存库数量、结存数量
	药品编号，药品名，批号，单位，库存数量，结存数量，盈亏数量
	1:storeCountId,storeCountDate,storeCountPerson,storeCountStoreId
	2:storeCountDetId,countDetCountId,storeCountDetBatchnumber,storeCountDetInputNum,storeCountDetMedId,
	storeCountDetMedName,storeCountDetMedUnit,storeCountDetStockNumber,storeCountDetYkNum -->
	<div id="yzr-display-count-none">
	<form id="yzr_pandian_form" method="post" style="padding:30px;background:#ECF3FF;">	
		<table style="width:100%;">
			<tr>
				<td style="width:42%;">
					<select class="easyui-combogrid" id="yzr-pandian-store"
						data-options="
							label: '盘点仓库',
							labelAlign:'right',
							required:true,
							width: '95%',
							panelWidth: '290',
							panelHeight: 'auto',
							url: 'queryAllStorehouse',
							pagination: true,
							pageSize: 8,
							pageList: [8, 16, 24, 32],
							idField: 'storeId',
							textField: 'storeName',
							columns: [[				
								{field:'storeName',title:'仓库名',width:100},
								{field:'storeKind',title:'类型',width:85},
								{field:'storeAddress',title:'地址',width:100}
							]]
					"></select>
				</td>
				<td style="width:42%;">
					<select class="easyui-combogrid" id="yzr-pandian-staff"
						data-options="
							label: '盘点员',
							labelAlign:'right',
							required:true,
							width: '95%',
							panelWidth: '290',
							panelHeight: 'auto',
							url: 'queryAllStaff',
							pagination: true,
							pageSize: 8,
							pageList: [8, 16, 24, 32],
							idField: 'staffNo',
							textField: 'staffName',
							columns: [[
								{field:'staffName',title:'姓名',width:100},
								{field:'departmentName',title:'部门',width:100},
								{field:'postName',title:'职务',width:85}
							]]
					"></select>
				</td>
				<td style="width:16%;"></td>
			</tr>
			
			<tr>
				<td>
					<input id="yzr-pandian-med" class="easyui-textbox" data-options="required:true,width: '75%',labelAlign:'right',label:'药品'"/>
					<a id="yzr-pandian-btn" href="#" class="easyui-linkbutton" data-options="width: '19%',iconCls:'icon-search'">查找</a>
				</td>
				<td>
					<input id="yzr-pandian-pihao" class="easyui-textbox" data-options="required:true,width: '95%',labelAlign:'right',readonly:true,label:'批号'"/>
				</td>
				
			</tr>
			
			<tr>
				<td>
					<input id="yzr-pandian-kcnumber" class="easyui-textbox" data-options="required:true,width: '95%',labelAlign:'right',readonly:true,label:'库存数量'"/>
				</td>
				<td>
					<input id="yzr-pandian-jiecunnumber" class="easyui-numberbox" data-options="required:true,width: '95%',labelAlign:'right',label:'盘点数量'"/>
				</td>
				<td>
					<a id="yzr-pandian-submit" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</a>
					<a id="yzr-pandian-cancel" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">删除</a>
				</td>
			</tr>
		</table>
	</form>
	</div>
	
	<div style="margin: 5px;">
		<div id="yzr-pandianDet-tabs" class="easyui-tabs" style="width:100%; margin-top:20px;" data-options="plain:true,narrow:true">
			
			
			<!-- tab1 -->
			<div title="盘点药品" style="padding:10px;">
					<table id="yzr-pandianDet-dg" class="easyui-datagrid" 
							data-options="
								iconCls: 'icon-edit',
								singleSelect:true,	
								toolbar: '#yzr-pandianDet-tb',							
								width: '100%'
								">
							<thead>
								<tr><!-- 药品编号，药品名，批号，单位，库存数量，结存数量，盈亏数量 
										2:storeCountDetId,countDetCountId,storeCountDetBatchnumber,storeCountDetInputNum,storeCountDetMedId,
										storeCountDetMedName,storeCountDetMedUnit,storeCountDetStockNumber,storeCountDetYkNum-->	
									<th data-options="field:'storeCountDetMedId',width:'20%',align:'center'">药品编号</th>			
									<th data-options="field:'storeCountDetMedName',width:'20%',align:'center'">药品名称</th>
									<th data-options="field:'storeCountDetBatchnumber',width:'15%',align:'center'">批号</th>
									<th data-options="field:'storeCountDetMedUnit',width:'12%',align:'center'">单位</th>
									<th data-options="field:'storeCountDetStockNumber',width:'12%',align:'center'">库存数量</th>
									<th data-options="field:'storeCountDetInputNum',width:'11%',align:'center'">盘点数量</th>
									<th data-options="field:'storeCountDetYkNum',width:'11%',align:'center'">盈亏数量</th>
								</tr>
							</thead>
					</table>
					
					<div id="yzr-pandianDet-tb">								
						<a id="yzr-pandianDet-tijiao" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-ok'">提交</a>			
					</div>
			 </div>	
			 
			<!-- tab2 -->
			<div  title="查看盘点详情" style="padding:10px;">
				<table id="yzr-count-detail-dg" class="easyui-datagrid" 
					data-options="
						url: 'queryAllCountYzr',
						iconCls: 'icon-edit',	
						pagination: true,
						singleSelect: true,
						toolbar: '#yzr-count-detail-tb',
						pageSize: 16,
						pageList: [16, 32, 48, 54],						
						width: '100%',
						height: '400px'
						">
					<thead>
						<tr><!-- 盘点编号，盘点日期，盘点人，仓库
								storeCountId,storeCountDate,storeCountPerson,storeCountStoreId-->	
							<th data-options="field:'XXX',width:'4%',checkbox:true,align:'center'">药品编号</th>			
							<th data-options="field:'storeCountId',width:'28%',align:'center'">盘点编号</th>
							<th data-options="field:'staffName',width:'23%',align:'center'">盘点人</th>
							<th data-options="field:'storeName',width:'23%',align:'center'">盘点仓库</th>
							<th data-options="field:'storeCountDate',width:'24%',align:'center'">盘点日期</th>
						</tr>
					</thead>
				</table>
				<div id="yzr-count-detail-tb">
					<a id="yzr-count-detail-more" class="easyui-linkbutton" data-options="iconCls:'icon-more',plain:true">查看详情</a>
					<a id="yzr-count-detail-reload" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a>					
				</div> 
			</div>							
		</div>						
	</div>
	
	
	
	<!--查看详情窗口 -->
	<div id="yzr-countMed-detail-window" class="easyui-window" title="查看盘点详情" data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true" style="width:70%;height:70%;">		
		<div class="easyui-layout" style="padding:0px;" data-options="fit:true">			
			<div data-options="region:'center'" style="padding:5px;border-top:none;border-left:none;border-right:none;">	
				
				<form method="post" style="margin:10px;width:98%;">
					<table width="98%" style="margin:10px;">
						<tr>
							<td>盘点编号:<a id="yzr-countMed-detail-pdbh" name="purchId" class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>盘点仓库:<a id="yzr-countMed-detail-pdck" name="purchTitle" class="easyui-linkbutton" data-options="plain:true"></a></td>
							
						</tr>
						<tr>
							<td>盘点人:<a id="yzr-intock-detail-pdr" name="staffNameSp" class="easyui-linkbutton" data-options="plain:true"></a></td>
							<td>盘点日期:<a id="yzr-intock-detail-pdrq" name="purchAutditState" class="easyui-linkbutton" data-options="plain:true"></a></td>
							
						</tr>
					</table>
						
					<div style="margin: 10px;">
						<div id="yzr-countMed-tabs" class="easyui-tabs" style="width:98%; margin-top:10px;" 
							data-options="plain:true,narrow:true">
							
							<div title="入库药品明细" style="padding:10px;">
								<table id="yzr-countMed-detail-dg" class="easyui-datagrid"  style="margin-left:10px"
										data-options="
											iconCls: 'icon-edit',
											singleSelect: true,
											align: 'center',										
											width: '98%'
											">
										<thead>
											<tr>	<!--盘点详情编号，盘点编号，药品编号，药品名称，批号，单位，库存数量，结存数量，盈亏数量
											////storeCountDetId,countDetCountId,storeCountDetBatchnumber,storeCountDetInputNum,storeCountDetMedId,
											storeCountDetMedName,storeCountDetMedUnit,storeCountDetStockNumber,storeCountDetYkNum  -->
												<th data-options="field:'storeCountDetId',width:'20%',align:'center'">盘点详情编号</th>	
												<th data-options="field:'storeCountDetMedName',width:'20%',align:'center'">药品名称</th>
												<th data-options="field:'storeCountDetBatchnumber',width:'15%',align:'center'">批号</th>
												<th data-options="field:'storeCountDetMedUnit',width:'12%',align:'center'">单位</th>
												<th data-options="field:'storeCountDetStockNumber',width:'12%',align:'center'">库存数量</th>
												<th data-options="field:'storeCountDetInputNum',width:'12%',align:'center'">结存数量</th>
												<th data-options="field:'storeCountDetYkNum',width:'11%',align:'center'">盈亏数量</th>
											</tr>
										</thead>
								 </table>
							</div>						
						</div>				
					</div>
				</form>								
			</div>
					
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel', plain:true" href="javascript:$('#yzr-countMed-detail-window').window('close');" onclick="">取消</a>
			</div>			
		</div>
	</div>
	
	
	
	<!-- 选择药品窗口 -->
	<div id="yzr-selectMed-window" class="easyui-window" title="选择盘点药品" style="width:850px;height:350px;" data-options="closable:true,closed:true,collapsible:false,minimizable:false,maximizable:false">
		<table id="tb-medStore-selectMed" class="easyui-datagrid"
			data-options="				
				fit:true,
				pagination: true,
				pageSize: 8,
				pageList: [8, 16, 24, 32],
				rownumbers: false,
				toolbar: '#yzr-selectMed-tb',
				border:false,
				singleSelect:true
			">
			<thead>
				<tr>	
				<!-- 库存编号，仓库编号，药品编号，药品名，批号，类型（中药，西药），规格，单位，单价，数量，金额	
				//medStockId,medStockBatchnumber,medStockGuige,medStockMedId,medStockMedKind,medStockMedName,medStockMoney,
				//medStockNumber,medStockPrice,medStockStoreId,medStockUnit -->
					<!-- <th data-options="field:'medStockId',width:'15%',align:'center'">库存编号</th> -->
					<th data-options="field:'storeName',width:'18%',align:'center'">仓库</th>
					<th data-options="field:'medStockMedId',width:'18%',align:'center'">药品编号</th>
					<th data-options="field:'medStockMedName',width:'18%',align:'center'">药品名</th>
					<th data-options="field:'medStockMedKind',width:'10%',align:'center'">类型</th>	
					<th data-options="field:'medStockBatchnumber',width:'10%',align:'center'">批号</th>					
					<th data-options="field:'medStockGuige',width:'10%',align:'center'">规格</th>	
					<th data-options="field:'medStockUnit',width:'8%',align:'center'">单位</th>		
					<th data-options="field:'medStockNumber',width:'10%',align:'center'">数量</th>									
				</tr>
			</thead>
		</table>				    
		    
		<div id="yzr-selectMed-tb">		
			<input id="yzr-selectMed-search" class="easyui-searchbox" data-options="menu: '#yzr-selectMed-pName', width: 220, prompt: '请输入关键字'"/>
			<a id="yzr-selectMed-reload" class="easyui-linkbutton" data-options="iconCls:'icon-reload'">刷新</a>
			<a id="yzr-ok-btn-selectMed" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="margin-left:475px;">确定</a>			
		</div>
		
		<div id="yzr-selectMed-pName" style="width:140px">
		    <div data-options="name:'name'">药品名称</div>
		</div>
	</div>		
</body>
</html>